//
//  DetailTableViewCell.m
//  Comma
//
//  Created by 张诗雨 on 15/1/21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailTableViewCell.h"
#import "GlobalMacro.h"
#import "SETextView.h"
#import "GDataXMLNode.h"
#import "FaceModel.h"
#import "GlobalMethod.h"
#import "EditImage.h"
@interface DetailTableViewCell ()<UIWebViewDelegate>

@end

@implementation DetailTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 设置选中效果
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        self.infoDic = [NSMutableDictionary dictionary];
        self.replyButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.replyButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
        [self.replyButton setFrame:CGRectMake(24, 0, 0, 0)];
        [self.replyButton addTarget:self action:@selector(replyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.replyButton];
        
//        self.replyLabel = [[UITextView alloc] init];
//        self.replyLabel.font = [UIFont systemFontOfSize:14.0f];
//        [self.replyLabel setTextColor:TEXTERGRAY];
//        [self.replyLabel clipsToBounds];
//        [self.replyLabel setEditable:NO];
//        [self.replyLabel setFrame:CGRectMake(24, 6, 0, 0)];
//        [_replyLabel setBackgroundColor:[UIColor grayColor]];
//        [self.contentView addSubview:self.replyLabel];
        
        self.replyView = [[SETextView alloc] init];
        [self.replyView setBackgroundColor:[UIColor whiteColor]];
        [self.contentView addSubview:self.replyView];
        
        self.timeLabel = [[UILabel alloc] init];
        [self.timeLabel setBackgroundColor:[UIColor whiteColor]];
        [self.contentView addSubview:self.timeLabel];
        [_timeLabel setFont:[UIFont systemFontOfSize:10]];
        [_timeLabel setTextColor:TEXTERGRAY];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.replyView.text = @"";
    [self.replyButton sizeToFit];
    NSString *text = [self.infoDic objectForKey:@"content"];
    NSArray * resultArr = [FaceModel getImageByString:text];
    
//    NSLog(@"%@", resultArr);
    NSInteger count = [resultArr count];
    for (int i = count - 1; i >= 0; i --) {
        id element = [resultArr objectAtIndex:i];
        if ([element isKindOfClass:[NSAttributedString class]]) {
            [self.replyView insertAttributedText:element];
        }
        else if ([element isKindOfClass:[UIImage class]]) {
            EditImage * image = [[EditImage alloc] initWithData:UIImageJPEGRepresentation(element, 1.0)];
            image.name = @"123";
            [self.replyView insertObject:image size:CGSizeMake(35, 35)];
        }
    }

    [self.timeLabel sizeToFit];
    CGRect rect = _timeLabel.frame;
    [self.timeLabel setFrame:CGRectMake(SCREEN_WIDTH - rect.size.width - 8, _replyView.frame.origin.y, rect.size.width, _replyView.frame.size.height)];
    
    CGSize containerSize = CGSizeMake(SCREEN_WIDTH, 10000);
    CGSize contentSize = [self.replyView sizeThatFits:containerSize];
    self.replyView.frame = CGRectMake(self.replyButton.frame.origin.x, self.replyButton.frame.origin.y + self.replyButton.frame.size.height, SCREEN_WIDTH - _timeLabel.frame.size.width, contentSize.height);

//    frame.size.height = MAX(contentSize.height, containerSize.height);
//    [self.replyView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, frame.size.height)];
//
}



- (void)replyButtonAction:(id)sender
{
    [self.delegate detailReplyAction:[self.infoDic objectForKey:@"puberId"]];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end